<?php

namespace app\api\validate;

use think\Validate;

class Tool extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        'phone'=>'require|number',
        'phone_code'=>'require|number',
        'code'=>'require|number',
        'type'=>'require|number',
        'key'=>'require',
        'role'=>'require|number',
        'email'=>'require|email',
        'password'=>'require|compare',
        'repassword'=>'require|confirm:password',
        'country_id'=>'number',
        'scene'=>'require',
        'source_type'=>'require',
        'token'=>'requireIf:type,2',
        'user_id'=>'requireIf:type,1',
        'uuid'=>'require',
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */	
    protected $message = [
        'phone.require'=>'Phone cannot be empty',
        'phone.number'=>'Phone must be a number',
        'phone_code.require'=>'Phone code cannot be empty',
        'phone_code.number'=>'Phone code must be a number',
        'code.require'=>'Code cannot be empty',
        'code.number'=>'Code must be a number',
        'email.require'=>'Email cannot be empty',
        'scene.require'=>'Scene cannot be empty',
        'source_type.require'=>'Source type cannot be empty',
        'email.email'=>'Incorrect email format',
        'password.require'=>'Password cannot be empty',
        'repassword.require'=>'Repassword cannot be empty',
        'repassword.confirm'=>'Inconsistent passwords entered twice',
        'type.require'=>'Type cannot be empty',
        'key.require'=>'Key cannot be empty',
        'type.number'=>'Type must be a number',
        'role.require'=>'Role cannot be empty',
        'role.number'=>'Role must be a number',
        'country_id.number'=>'Country ID must be a number',
        'email.requireIf'=>'Email cannot be empty',
        'phone.requireIf'=>'Phone cannot be empty',
        'token.requireIf'=>'Token cannot be empty',
        'user_id.requireIf'=>'User ID cannot be empty',
        'uuid.require'=>'UUID cannot be empty',
    ];

    protected $scene=[
        'phone'=>['phone','code','phone_code'],
        'email'=>['email','code'],
        'send'=>['email','type','role'],
        'password'=>['password','repassword'],
        'airports'=>['country_id'],
        'materials'=>['type'],
        'upload'=>['scene','source_type'],
        'uploads'=>['scene'],
        'content'=>['key'],
        'sendSms'=>['phone','phone_code','type','role'],
        'code'=>['code','email','type','role'],
        'qrCode'=>['uuid','token','type','user_id'],
        'getCode'=>['uuid'],
    ];

    protected function compare($value,$rule,$data=[])
    {
        if(isset($data['password']) && !isset($data['repassword'])){
            return 'Repassword cannot be empty';
        }

        //定义正则匹配规则
        $pattern = '/^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*]{8,}$/';
        //使用preg_match()函数进行匹配
        if (!preg_match($pattern, $data['password'])) {
            return 'The password must contain letters, numbers, and be at least 8 digits in length';
        }

        return true;
    }

//    public function sceneCode(){
//        return $this->only(['code','type','phone','email'])
//            ->remove('phone','require')
//            ->remove('email','require')
//            ->append('email','requireIf:type,'.config('data.code_type.forget'))
//            ->append('phone','requireIf:type,'.config('data.code_type.email'));
//    }
}
